/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.common.mixin.core.item.recipe.crafting;

import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.Ingredient;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.item.inventory.util.ItemStackUtil;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Mixin(Ingredient.class)
@Implements(@Interface(iface = org.spongepowered.api.item.recipe.crafting.Ingredient.class, prefix = "ingredient$"))
public abstract class MixinIngredient {

    @Shadow @Final protected ItemStack[] matchingStacks;
    @Shadow public abstract boolean apply(ItemStack p_apply_1_);

    public List<org.spongepowered.api.item.inventory.ItemStackSnapshot> ingredient$displayedItems() {
        return Arrays.stream(matchingStacks).map(ItemStackUtil::snapshotOf).collect(Collectors.toList());
    }

    public boolean ingredient$test(org.spongepowered.api.item.inventory.ItemStack itemStack) {
        return this.apply(ItemStackUtil.toNative(itemStack));
    }

    /**
     * This method is needed to prevent the default methods in com.google.common.base.Predicate
     * and org.spongepowered.api.item.recipe.crafting.Ingredient from conflicting.
     *
     * Guava's Predicate defines a default method 'test' (overriding the Java 8 Predicate's 'test' method),
     * which delegates to 'apply'.
     * Since 'test' is generic over Predicate's type parameter, its signature
     * will be 'test(Object param)' after type erasure
     *
     * On Sponge's side, Ingredient 'overrides' the 'test' method from the Java 8 Predicate.
     * This causes a 'bridge method' to be generated by the compiler, which overrides
     * the type-erased method 'test(Object param)', and delegates to 'test(org.spongepowered.api.item.inventory.ItemStack stack)'
     *
     * When Sponge's Ingredient interface is mixed into the NMS Ingredient class, the method 'test(Object param)'
     * will be implemented by default methods in two different interfaces: Guava's Predicate, and Sponge's Ingredient.
     * At runtime, calling this method results in an error.
     *
     * To resolve this issue, we manually implement 'test(Object param)' here. Since method resolution order dictates
     * that methods defined in the class will be used before checking for interface default methods, calls to either Guava or
     * Sponge Ingredient's 'test' method will succeed.
     *
     */
    public boolean test(Object stack) {
        return this.apply((ItemStack) stack);
    }
}
